require 'task'
class Iteration < ActiveRecord::Base
  belongs_to :project
  has_many :meetings
  has_many :stories
  has_many :agile_tasks, :through => :stories
  
  def work_units_real
    #count w.u. for completed tasks only
    wu=0
    if not complete_tasks.empty?
      complete_tasks.each do |task|
        wu += task.work_units_real
      end
    end
    wu
  end
  
  def agile_tasks_number
    agile_tasks.size
  end
  
  def complete_tasks
    #finished tasks
    agile_tasks.find(:all, 
      :conditions=>"completion_date is not null", 
      :order=>"completion_date desc, updated_at desc")
  end

  def daily_load
    wu_todo= work_units - work_units_real
    if wu_todo > 0
      return wu_todo/days_left
    else
      return 0
    end
  end
  
private
  
  def days_left
    #amount of days left to the end of the iteration
    end_date.-(Date.today).to_i + 1
  end
  
  
end
